package logic

import (
	"context"
	"database/sql"
	"errors"
	"fmt"

	"go-pod/pod-rpc/internal/svc"
	"go-pod/pod-rpc/pod"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetPodcastLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetPodcastLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPodcastLogic {
	return &GetPodcastLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetPodcast 获取播客
func (l *GetPodcastLogic) GetPodcast(in *pod.GetPodcastReq) (*pod.GetPodcastResp, error) {
	res, err := l.svcCtx.PodcastModel.FindOne(l.ctx, uint64(in.Id))
	if errors.Is(err, sql.ErrNoRows) {
		return nil, fmt.Errorf(`sql: no rows in result set`)
	}
	return &pod.GetPodcastResp{
		Id:        int64(res.Id),
		Title:     res.Title.String,
		Tags:      res.Tags.String,
		Author:    res.Author.String,
		Content:   res.Content.String,
		CreatedAt: res.CreatedAt.Time.Format("2006-01-02 15:04:05"),
		UpdatedAt: res.UpdatedAt.Time.Format("2006-01-02 15:04:05"),
	}, nil
}
